package com.ll.springbucks.controller;

import com.ll.springbucks.controller.req.NewOrderRequest;
import com.ll.springbucks.model.Coffee;
import com.ll.springbucks.model.CoffeeOrder;
import com.ll.springbucks.model.RetXml;
import com.ll.springbucks.service.CoffeeOrderService;
import com.ll.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class CoffeeOrderController {
    @Autowired
    private CoffeeOrderService orderService;
    @Autowired
    private CoffeeService coffeeService;

    @PostMapping("/")
    @ResponseStatus(HttpStatus.CREATED)
    public CoffeeOrder create(@RequestBody NewOrderRequest newOrder) {
        log.info("Receive new Order {}", newOrder);
        Coffee[] coffeeList = coffeeService.getCoffeeByName(newOrder.getItems()).toArray(new Coffee[] {});
        return orderService.createOrder(newOrder.getCustomer(), coffeeList);
    }

    /**
     * 返回xml
     * @return
     */
    @GetMapping(value = "/getXml",  produces = "application/xml")
    public RetXml getXml() {
//        Optional<Coffee> cf = coffeeService.findOneCoffee("name");
//        Coffee coffee = cf.get();
//        Coffee.CoffeeBuilder coffee = Coffee.builder().name("nihao").price(Money.of(CurrencyUnit.of("CNY"), 100.0));
//        return coffee;

        RetXml info = new RetXml();
        info.setParam1("pm1");
        info.setParam2("pm2");
        return  info;
    }


}
